<?php
namespace php\widgets\editor;
use Yii;
class EditorWidget extends \yii\base\Widget{
	public $id;
	public $type;
	public $lang;
	public $data;

	private $_widgets = [
		'layoutWidget' => WebLayoutWidget::class,
		'editorWidget' => WebEditorWidget::class,
		'imagesWidget' => WebImagesWidget::class,
		'imageWidget' => WebImageWidget::class,
		'textWidget' => WebTextWidget::class,
		'textH1Widget' => WebTextH1Widget::class,
		'sliderWidget' => WebSliderWidget::class,
		'homeSliderWidget' => WebHomeSliderWidget::class,
		'collapseWidget' => WebCollapseWidget::class,
		'listContainerWidget' => WebListContainerWidget::class,
		'tabsWidget' => WebTabsWidget::class,
	];

	public function run(){
		$widgets = null;
		switch ($this->type) {
			case 'release':
				$widgets = json_decode($this->data, true);
				if(empty($widgets)){
					return \frontend\themes\chiron\widgets\editorxml\EditorWidget::widget(['type'=>$this->type,'data'=>$this->data,'lang'=>$this->lang, 'id'=>$this->id]);
				}
			break;
			
			default:
				$widgets = Yii::$app->pageService->getPreview($this->id, $this->lang);
			break;
		}
		$shtml = '';
		if(!empty($widgets)){
			foreach ($widgets as $key => $val) {				
				$shtml .= $this->renderWidget($val);
			}
		}

		return $shtml;
	}

	public function renderWidget($val, $parent_type = ''){
		if(isset($this->_widgets[$val['type']])){
			$widgetClass = $this->_widgets[$val['type']];
			$shtml = $widgetClass::widget([
				'value'=>isset($val['datas']['value']) ? $val['datas']['value'] : null, 
				'child'=>!empty($val['datas']['child']) ? $val['datas']['child'] : null,
				'that' => $this,
				'parent_type' => $parent_type,
			]);
			return $shtml;
		}	
		return null;
	}
}